<?php
/**
 * @author 
 * @copyright 2014
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Menu_model extends CI_Model
{
	var $arr_action=array();
	function __construct()
	{
		parent::__construct('menus');
        //$this->arr_action = $this->getTask();                
	}
    public function getMenu($current = ''){
        if(!empty($current)){
          $current = json_decode($current,true);
		}
        else{
          $current = array();
        }
		#region Lấy danh cha
		$sql    =   "  SELECT m.name as title, m.parent as parentid, m.route as link, m.id, m.params 
                        FROM menus m                      
                        WHERE m.parent = 0 
						and m.isdelete = 0 
						order by m.ordering ASC";
		$data =   $this->model->query($sql)->execute();
        #end
        $menu = '';
        $i = 0;
        $n = count($data);
        $rchk_c = 0;
        foreach($data as $value){
			$ids = $value->id;
			$title = $value->title;
            $asub = $this->getChildren($ids, $current);
            if($asub){
                $sub = $asub['sub'];
                $rchk = $asub['chk'];
                $menu.='{id : "node-'.$ids.'",
                            value : "parent-'.$ids.'",
                            text : "'.$title.'",
                            showcheck : true,
                            complete : true,
                            isexpand : true,
                            checkstate : '.$rchk.',
                            hasChildren : true,
                            ChildNodes : '.$sub.'
                            }';
                if($rchk==1 || $rchk==2) $rchk_c++;
            }
            else{
                $params = explode(',',$value->params);
                $str_param = '';
                $j =0;
                $m = count($params);
                $l = (isset($current[$ids]))?count($current[$ids]):0;
                foreach($params as $param){
                    if(isset($ids)){
                        $va = $ids;
					}
                    else{
                        $va = '';
					}
                    $chk = (isset($current[$ids][$param]))?1:0;
                    $str_param.='{id : "node-'.$va.'-'.$param.'",
                                value : "'.$ids.'-'.$param.'",
                                text : "'.$param.'",
                                showcheck : true,
                                complete : true,
                                isexpand : false,
                                checkstate : '.$chk.',
                                hasChildren : false}';
                    if($j<($m-1)) $str_param.=',';
                    $j++;
                }
                
                if($l < 1){
					$pchk = 0;
				}
                else {             
                    $pchk = ($l == $m)?1:2;
				}
                if($pchk==1){
					$rchk_c++;
				}
                $menu.='{id : "node-'.$ids.'",
                            value : "sub-'.$ids.'",
                            text : "'.$title.'",
                            showcheck : true,
                            complete : true,
                            isexpand : false,
                            checkstate : '.$pchk.',
                            hasChildren : true,
                            ChildNodes : ['.$str_param.']
                            }';
            }
            if($i<($n-1)) $menu.=',';
            $i++;
        }
        if($rchk_c<1){
			$rrchk = 0;
		}
        else{
            $rrchk = ($rchk_c == $n)?1:2;
		}
        return array('chk'=>$rrchk,'menu'=>'['.$menu.']');
    }
    private function getChildren($id, $current, $level=0){   
		$sql    =   "  SELECT m.name as title, m.parent as parentid, m.route as link, m.id, m.params 
                        FROM menus m                      
                        WHERE m.parent = '$id' 
						and m.isdelete = 0
						order by m.ordering ASC";
		$data =   $this->model->query($sql)->execute();
		$n = count($data);
        if($n > 0){
            $i = 0;
            $n = count($data);
            $menu = '';
            $rchk_c = 0;
            foreach($data as $value){
				$ids = $value->id;
				$title = $value->title;
                $asub = $this->getChildren($ids, $current, 1);
                if($asub){
                    $sub = $asub['sub'];
                    $rchk = $asub['chk'];
                   
                    $menu.='{id : "node-'.$ids.'",
                                value : "parentsub-'.$ids.'",
                                text : "'.$title.'",
                                showcheck : true,
                                complete : true,
                                isexpand : true,
                                checkstate : '.$rchk.',
                                hasChildren : true,
                                ChildNodes : '.$sub.'
                                }';
                    if($rchk==1 || $rchk==2) $rchk_c++;
                }
                else{
                    $params = explode(',',$value->params);
                    $str_param = '';
                    $j =0;
                    $m = count($params);
                    $l = (isset($current[$ids]))?count($current[$ids]):0;
                    foreach($params as $param){
                        $chk = (isset($current[$ids][$param]))?1:0;
                        $parent = (($level>0)?'-'.$value->parentid :'');
                        $str_param.='{id : "node-'.$ids.'-'.$param.$parent.'",
                                    value : "'.$ids.'-'.$param.$parent.'",
                                    text : "'.$param.'",
                                    showcheck : true,
                                    complete : true,
                                    isexpand : false,
                                    checkstate : '.$chk.',
                                    hasChildren : false}';
                        if($j<($m-1)) $str_param.=',';
                        $j++;
                    }
                    if($l < 1){$pchk = 0;}
                    else{                
                        $pchk = ($l == $m)?1:2;
                     } 
                    if($pchk>0){ $rchk_c++; }
                    $menu.='{id : "node-'.$ids.'",
                                value : "sub-'.$ids.'",
                                text : "'.$title.'",
                                showcheck : true,
                                complete : true,
                                isexpand : true,
                                checkstate : '.$pchk.',
                                hasChildren : true,
                                ChildNodes : ['.$str_param.']
                                }';
                }
                if($i<($n-1)) $menu.=',';
                $i++;
            }
            if($rchk_c<1){
				$rrchk = 0;
			}
            else{
                $rrchk = ($rchk_c == $n)?1:2;
            }
            return array('chk'=>$rrchk,'sub'=>'['.$menu.']');
        }
        return false;
    }
 }